//
//  CMDBBaseViewController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/6.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBBaseViewController: UIViewController,UIGestureRecognizerDelegate {
    
    let width : CGFloat  = UIScreen.mainScreen().bounds.width
    
    let height : CGFloat = UIScreen.mainScreen().bounds.height
    
    var enableInteractivePopGestureRecognizer : Bool?{
        
        get{
             return (self.navigationController?.interactivePopGestureRecognizer?.enabled)
        }
        
        set(newVal){
            self.navigationController?.interactivePopGestureRecognizer?.enabled = newVal!
        }
    }
    
    internal var dataBlock: CMDBDataBlock?
    

    override func viewDidLoad() {
        super.viewDidLoad()

        setup()
        
    }
    
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        
        return UIStatusBarStyle.LightContent
        
    }
    
    /**
        setup UI
     */
    func setup(){

        automaticallyAdjustsScrollViewInsets = false
        
        view.backgroundColor                 = UIColor.whiteColor()
        
    }
    
    /** 
        use this method when the controller is an NavigationController's rootVC
     */
    func useInteractivePopGestureRecogmizer(){
        
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    func popViewControllerAnimated(animated : Bool){
        
        self.navigationController?.popViewControllerAnimated(animated)
        
    }
    
    
    func popToRootViewControllerAnimated(animated : Bool){
        
        self.navigationController?.popToRootViewControllerAnimated(animated)
        
    }
    
    deinit{
        
        print("[ X ] '" + String(self.classForCoder) + "' is released")
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        print("[ → ] enter to --> '" + String(self.classForCoder) + "'.")
    }
    
    override func viewDidDisappear(animated: Bool) {
        
        super.viewDidDisappear(animated)
        print("[🕐] leave from <-- '" + String(self.classForCoder) + "'.")
    }
    

}


//MARK : - 屏幕相关
extension CMDBBaseViewController {

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Portrait
    }
    
    override func shouldAutorotate() -> Bool {
        return false
    }
    
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return .Portrait
    }
    
}

extension CMDBBaseViewController: CMDBBaseViewDelegate {
    
    func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {


    }
}

extension CMDBBaseViewController{
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        
        view.endEditing(true)
    }
}